//
//  ALPRankViewController.m
//  AlpFM
//
//  Created by KING on 16/1/25.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPRankViewController.h"
#import "ALPTopListTableViewCell.h"
#import <AFNetworking.h>
#import "ALPTopModel.h"
#import "ALPTopHotViewController.h"
#import "ALPOtherHotViewController.h"
@interface ALPRankViewController ()
<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *allArray;
@end

@implementation ALPRankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -90-64) style:UITableViewStylePlain];
    //    self.myTableView.backgroundColor = [UIColor colorWithRed:1.000 green:0.599 blue:0.260 alpha:1.000];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[ALPTopListTableViewCell class] forCellReuseIdentifier:@"top"];
    //取消边界反弹效果
    self.myTableView.bounces = NO;
    
    self.allArray = [NSMutableArray array];
    [self p_headerView];
    [self p_getData];

    
}
#pragma mark - 轮播图
- (void)p_headerView{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.543*SCREEN_WIDTH)];
    NSURL *url = [NSURL URLWithString:@"http://fdfs.xmcdn.com/group16/M06/DC/E0/wKgDalaKEZbxsfSyAANGoFGmeKA452_android_large.jpg"];
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"girl"]];
    self.myTableView.tableHeaderView = imageView;
    
}

#pragma mark - 协议方法 高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0.267*SCREEN_WIDTH;
}

#pragma mark - 协议方法 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    NSLog(@"all%ld", self.allArray.count);
    return self.allArray.count - 3;
}

#pragma mark - 协议方法 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ALPTopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"top"];
    cell.model = [self.allArray objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - 获取数据
- (void)p_getData{
    
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-d3&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2"];
    //    NSLog(@"%@", url);
    
    //中文解码
    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    [session GET:urlEncode parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //        NSLog(@"成功");
#pragma mark -- 列表栏赋值
        NSMutableArray *dataArray = [responseObject objectForKey:@"datas"];
        
        for (NSMutableDictionary *dic in dataArray) {
            NSMutableArray *array = [dic objectForKey:@"list"];
            for (NSMutableDictionary *dicMin in array) {
                ALPTopModel *model = [[ALPTopModel alloc] init];
                [model setValuesForKeysWithDictionary:dicMin];
//                ALPTopModel *model = [[ALPTopModel alloc] initWithDictionary:dicMin];
                [self.allArray addObject:model];
            }
        }
        
        [self.myTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"失败");
        
    }];

    
//    [AFN1027 finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
//        //        NSLog(@"%@", resuposeObject);
//        
//#pragma mark -- 列表栏赋值
//        NSMutableArray *dataArray = [resuposeObject objectForKey:@"datas"];
//        
//        for (NSMutableDictionary *dic in dataArray) {
//            NSMutableArray *array = [dic objectForKey:@"list"];
//            for (NSMutableDictionary *dicMin in array) {
//                HFTopListModel *model = [[HFTopListModel alloc] initWithDictionary:dicMin];
//                [self.allArray addObject:model];
//                [model release];
//            }
//        }
//        
//        [self.myTableView reloadData];
//    } andError:^(NSError *error) {
//        
//    }];
}
#pragma mark - 选中执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    NSLog(@"index = %ld", indexPath.row);
    ALPTopModel *model = [self.allArray objectAtIndex:indexPath.row];
    //    NSLog(@"orderNum = %@", model.orderNum);
    switch (model.orderNum.intValue) {
        case 1:
        {
            ALPTopHotViewController *vc = [[ALPTopHotViewController alloc] init];
            vc.key = model.key;
            vc.listTitle = @"最火节目飙升榜";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            ALPOtherHotViewController *vc = [[ALPOtherHotViewController alloc] init];
            vc.key = model.key;
            vc.listTitle = @"最多收藏经典榜";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            ALPOtherHotViewController *vc = [[ALPOtherHotViewController alloc] init];
            vc.key = model.key;
            vc.listTitle = @"有声小说风云榜";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            ALPOtherHotViewController *vc = [[ALPOtherHotViewController alloc] init];
            vc.key = model.key;
            vc.listTitle = @"最受欢迎公开课榜";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
